home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SpriteEngine / SE Random / SpriteHanders.c < prev    next >
C/C++ Source or Header  |  1995-03-11  |  3KB  |  117 lines

  1. #include "SpriteTools.h"
  2.  
  3. // SpriteTools.h includes SpriteHandlers.h
  4.  
  5.  
  6. /*** Custom handlers - application dependent ***
  7.  
  8. Edit this as necessary. It should always include the following three routines:
  9.  
  10. MoveSprite: move the sprite
  11.  
  12. HitSprite: handle collisions between two sprites
  13.  
  14. InitSprites: Load all faces and create initial sprites
  15.  
  16. ***/
  17.  
  18.  
  19. GrafPtr    randomPositionFace, randomSpeedFace, changeSometimesFace, randomImpulseFace;
  20.  
  21.  
  22. void MoveSprite(SpritePtr theSprite)
  23. {
  24.     
  25.     switch (theSprite->kind)
  26.     {
  27.         case randomPositionSprite:
  28.             theSprite->position.h += Rand(7) - 3;
  29.             theSprite->position.v += Rand(7) - 3;
  30.             KeepOnScreen(theSprite);
  31.             break;
  32.         case randomSpeedSprite:
  33.             theSprite->fixedPointPosition.h += theSprite->speed.h;
  34.             theSprite->fixedPointPosition.v += theSprite->speed.v;
  35.             theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
  36.             theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
  37.             if (KeepOnScreenFixed(theSprite))
  38.             {
  39.                 theSprite->speed.h -= Sgn(theSprite->speed.h);
  40.                 theSprite->speed.v -= Sgn(theSprite->speed.v);
  41.             };
  42.             theSprite->speed.h += Rand(15) - 7;
  43.             theSprite->speed.v += Rand(15) - 7;
  44.             break;
  45.         case changeSometimesSprite:
  46.             theSprite->fixedPointPosition.h += theSprite->speed.h;
  47.             theSprite->fixedPointPosition.v += theSprite->speed.v;
  48.             theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
  49.             theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
  50.             KeepOnScreenFixed(theSprite);
  51.             if (Rand(500) < 10)
  52.             {
  53.                 theSprite->speed.h = Rand(63) - 31;
  54.                 theSprite->speed.v = Rand(63) - 31;
  55.             }
  56.             break;
  57.         case randomImpulseSprite:
  58.             theSprite->fixedPointPosition.h += theSprite->speed.h;
  59.             theSprite->fixedPointPosition.v += theSprite->speed.v;
  60.             theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
  61.             theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
  62.             KeepOnScreenFixed(theSprite);
  63.             theSprite->speed.h = theSprite->speed.h * 19 / 20;
  64.             theSprite->speed.v = theSprite->speed.v * 19 / 20;
  65.             if (theSprite->speed.h == 0 && theSprite->speed.v == 0)
  66.             {
  67.                 theSprite->speed.h = Rand(511) - 255;
  68.                 theSprite->speed.v = Rand(511) - 255;
  69.             }
  70.             break;
  71.     }
  72.  
  73. } /*MoveSprite*/
  74.  
  75.  
  76. void HitSprite(SpritePtr theSprite, SpritePtr anotherSprite)
  77. {
  78.     
  79. } /*HitSprite*/
  80.  
  81.  
  82.  
  83. void InitSprites()
  84. {
  85.     SpritePtr    theSprite;
  86.  
  87. /*Load all pictures*/
  88.     randomPositionFace = LoadFaceFromCicn(128);        /*cicn resource #128.*/
  89.     randomSpeedFace = LoadFaceFromCicn(129);        /*cicn resource #129.*/
  90.     changeSometimesFace = LoadFaceFromCicn(130);    /*cicn resource #130.*/
  91.     randomImpulseFace = LoadFaceFromCicn(131);        /*cicn resource #131.*/
  92.  
  93. /*Create sprites*/
  94.     theSprite = NewSprite();
  95.     theSprite->kind = randomPositionSprite;
  96.     theSprite->face = randomPositionFace;
  97.     SetPt(&theSprite->position, 50, 50);
  98.  
  99.     theSprite = NewSprite();
  100.     theSprite->kind = randomSpeedSprite;
  101.     theSprite->face = randomSpeedFace;
  102.     SetPt(&theSprite->fixedPointPosition, 100<<4, 100<<4);
  103.     SetPt(&theSprite->speed, 0, 0);
  104.  
  105.     theSprite = NewSprite();
  106.     theSprite->kind = changeSometimesSprite;
  107.     theSprite->face = changeSometimesFace;
  108.     SetPt(&theSprite->fixedPointPosition, 150<<4, 150<<4);
  109.     SetPt(&theSprite->speed, 0, 0);
  110.  
  111.     theSprite = NewSprite();
  112.     theSprite->kind = randomImpulseSprite;
  113.     theSprite->face = randomImpulseFace;
  114.     SetPt(&theSprite->fixedPointPosition, 200<<4, 200<<4);
  115.     SetPt(&theSprite->speed, 0, 0);
  116. } /*InitSprites*/
  117.